<template>
	<view>
		<!--地图-->
		<Map class="map" ref="mapson"></Map>
		<!--会员-->
		<!--dom渲染-->
		<!-- <Cars @fatherMethod="CallCarsMap"></Cars> -->
<!-- 		<view class="car-wrap">
		<button class="cu-btn bg-black round lg"  @click="CallCarsMap2()" @touchstart="showDeleteButton()" @touchend="emptyTime">{{nums}}</button>
		</view>
		<view class="car-driver">
		<button class="cu-btn bg-black round lg"  @click="DriverGo()">司机接单</button>
		</view> -->
	</view>
</template>

<script>
import Map from '../aMap';
import Cars from '../cars/cars';
export default {
	name: 'Index',
	components: {
		Map,
		Cars
	},
	data() {
		return {
			timer:null,//定时刷新任务
			Loop:null,   //这是一个按钮时长定时器
			callTF:false,
			nums:'请求车辆',
			start: '104.068818,30.60138',
			end: '103.969563,30.31744',
			price: '',
			userId:5,
			specSelected: {
				startAddress:'成都蜗牛学院天府校区',
				endAddress:'成都天府广场',
				rectangle: '',
				distance: null, //距离
				trucksId:'',
			}
		};
	},
	async onLoad(options) {
		const specSelected = JSON.parse(decodeURIComponent(options.specSelected));
		this.specSelected = specSelected;
		// console.log('传来的参为'+specSelected.trucksId);
		// console.log('赋值后为'+this.specSelected.trucksId);
		this.Howmoeny(specSelected);
		this.callTF=false;
	},
	methods: {
		CallCarsMap2() {
				console.log('点击了请求');
			if(this.callTF==false){
			var ss=this;
		// console.log('触发了请求车辆',ss.specSelected.trucksId);
			uni.showModal({
				title: '订单信息',
				content: '您的订单预估金额为' + ss.price+'是否要下单',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定',ss.specSelected);
						ss.doOrder();
						ss.callTF=true;
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
			}
		},
		//在方法中将以下方法复制过去即可应用。
		showDeleteButton() {
		    clearTimeout(this.Loop); //再次清空定时器，防止重复注册定时器
			var ss=this;
		    this.Loop = setTimeout(function() {
			this.underOrder();
		    }.bind(this),1000);
		},
		//清空
		emptyTime() {
		    clearTimeout(this.Loop); //清空定时器，防止重复注册定时器
		},
		Howmoeny(specSelected) {
			uni.request({
				url: 'https://restapi.amap.com/v3/direction/walking?origin='+this.start+'&destination='+this.end+'&key=1f5cfb770d01a1a0d8289169188de6b3',
				// data:{
				// 	origin: this.start,
				// 	destination: this.end,
				// 	key:'1f5cfb770d01a1a0d8289169188de6b3',
				// },
				success: res => {
					if (res.data.status) {
						// console.log('计算价格的请求进来了');
						this.specSelected.distance =res.data.route.paths[0].distance;
						// console.log('路程距离为'+this.specSelected.distance);
						uni.request({
							url: this.$ip +'order/Howmoney',
							method: 'POST',
							dataType: 'json',
							data: {
								uid: specSelected.userId,
								price: specSelected.price,
								tid: specSelected.trucksId,
								distance: this.specSelected.distance,
							},
							success: res => {
								if (res.data.flag) {
									this.price = res.data.data;
								console.log(res);
								}
							}
						});
					}
				}
			});
		},
		doOrder(){
			console.log('向后端传值'+this.specSelected);
			uni.request({
				url: this.$ip+'order/doOrder',
				method: 'POST',
				dataType: 'json',
				data: {
					order: {
						uid:this.userId,
						tid:this.specSelected.trucksId,
						cid:this.specSelected.comId,
						totalAmount:this.price,
						distance: this.specSelected.distance,
					},
					address: {
						startAddress: this.specSelected.startAddress,
						endAddress: this.specSelected.endAddress,
						uidxy: this.specSelected.rectangle,
					}
				},
				success: res => {
					if (res.data.flag) {
						this.timeSave();
					}
				}
		})
	},
	//计时器
	timeSave() {
				var my_self = this;
				this.nums=null;
				var hour, minute, second; /*时 分 秒*/
				hour = minute = second = 0; //初始化
				var millisecond = 0; //毫秒
				this.timer = setInterval(() => {
					millisecond = millisecond + 50;
					// console.log("---millisecond----"+millisecond);
					if (millisecond >= 1000) {
						millisecond = 0;
						second = second + 1;
					}
					if (second >= 60) {
						second = 0;
						minute = minute + 1;
					}
	 
					if (minute >= 60) {
						minute = 0;
						hour = hour + 1;
					}
					// console.log("-------"+hour+'时'+minute+'分'+second+'秒');
					this.nums = hour+'时'+minute+'分'+second+'秒';
				}, 50);
		},
	endtime() {
		 clearInterval(this.timer);
	},
	//取消订单的方法
	underOrder(){
		var ss=this;
		uni.showModal({
			title: '取消订单',
			content: '确定要取消订单吗',
			success: function(res) {
				if (res.confirm) {
				console.log('确定取消订单'+ss.callTF);
					ss.callTF=false;
					ss.endtime();//停止
					ss.nums='请求车辆';
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		})
		},
		//司机的方法
		DriverGo(){
			console.log('司机订单列表前往')
			uni.navigateTo({
				url:'/pages/movehouse/dirver/dirver',
			});
		}
	},
};
</script>

<style lang="scss">
.map {
	height: 85vh;
	position: relative;
}
.car-wrap{
	position: absolute;
	left: 100px;
	right: 0;
	bottom: 120px;
	z-index: 100;

}
.car-driver{
	position: absolute;
	left: 300px;
	right: 0;
	bottom: 120px;
	z-index: 100;
}
</style>
